package cn.softgame.cms.secure.dictionary.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.softgame.cms.common.page.domain.PageResultBean;
import cn.softgame.cms.open.common.service.CommonService;
import cn.softgame.cms.secure.dictionary.domain.Dictionary;
import cn.softgame.cms.secure.dictionary.service.DictionaryService;





/**
 * 字典控制器
 * @author zhaoruiqing
 * 2013-5-3
 */
@Controller
@RequestMapping("/secure/dictionary/*")
public class DictionaryController {
	@Autowired
	private DictionaryService dictService;
	
	@Autowired
	private CommonService commonService;
	
	/**
	 * 显示数据字典页
	 * @return
	 */
	@RequestMapping("listDictionaries.html")
	public ModelAndView listDictionaries(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("dict",new Dictionary());
		mav.setViewName("secure/dictionary/listDictionary");
		return mav;
	}
	
	@RequestMapping("qryPageDictionaries.html")
	public @ResponseBody PageResultBean qryPageDicts(Integer jtStartIndex,Integer jtPageSize,String dictName,String dictCode){
		PageResultBean pr = new PageResultBean();
		try {
			Dictionary dict=new Dictionary();
			dict.setDictName(dictName);
			dict.setDictCode(dictCode);
			dict.setFromNumber(jtStartIndex);
			dict.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(dictService.selectPageDictionaries(dict));
			pr.setTotalRecordCount(dictService.dictionaryCount(dict));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		return pr;
	}
	
	/**
	 * 弹出新增对话框
	 * @return dictionaryForm.jsp
	 */
	@RequestMapping("initDictionary.html")
	public ModelAndView initDict(){
		ModelAndView mav = new ModelAndView();
		mav.addObject("dict",new Dictionary());
		mav.setViewName("secure/dictionary/dictionaryForm");
		return mav;
	}
	
	@RequestMapping("saveDictionary.html")
	public @ResponseBody PageResultBean saveDict(Dictionary dict){
		PageResultBean pr=new PageResultBean();
		try {
			if(dict!=null){
				String dictId = dict.getDictId();
				if(dictId!=null && !"".equals(dictId)){
					dictService.updateDict(dict);
				}else{
					dict.setDictId(commonService.getCode());
					dictService.saveDict(dict);
				}
			}
			pr.setResult("OK");
			pr.setRecord(dict);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("保存失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="updateDictionary.html")
	public ModelAndView updateDict(String dictId){
		ModelAndView mav = new ModelAndView();
		mav.addObject("dict",dictService.selectDictById(dictId));
		mav.setViewName("secure/dictionary/dictionaryForm");
		return mav;
	}
	
	@RequestMapping("delDictionary.html")
	public @ResponseBody PageResultBean delDict(String dictId){
		PageResultBean pr = new PageResultBean();
		try {
			dictService.deleteDict(dictId);
			pr.setResult("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("删除失败!");
		}
		return pr;
	}
}
